//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ConvertibleBond.h"
using namespace Cephei::QL::Experimental;
#include <gen/QL/Instruments/Callability.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Cashflows/Dividend.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/CashFlow.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/Bond.h>
using namespace Cephei::QL::Instruments;
using namespace Cephei::QL;
using namespace Cephei::QL::Cashflows;
using namespace Cephei::QL::Times;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::CConvertibleBond::CConvertibleBond (boost::shared_ptr<QuantLib::ConvertibleBond>& childNative, Object^ owner) : CBond(CConvertibleBond::typeid)
{
#ifdef HANDLE
	_phConvertibleBond = NULL;
#endif
	_ppConvertibleBond = &childNative;
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppConvertibleBond));
}
Cephei::QL::Experimental::CConvertibleBond::CConvertibleBond (QuantLib::ConvertibleBond& childNative, Object^ owner) : CBond(CConvertibleBond::typeid)
{
#ifdef HANDLE
	_phConvertibleBond = NULL;
#endif
	_ppConvertibleBond = new boost::shared_ptr<QuantLib::ConvertibleBond> (&childNative);
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppConvertibleBond));
    _ConvertibleBondOwner = owner;
    _BondOwner = owner;
}

Cephei::QL::Experimental::CConvertibleBond::CConvertibleBond (CConvertibleBond^ copy) : CBond(CConvertibleBond::typeid)
{
#ifdef HANDLE
	_phConvertibleBond = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppConvertibleBond = new boost::shared_ptr<QuantLib::ConvertibleBond> (copy->GetShared());
        _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppConvertibleBond));
    }
}
Cephei::QL::Experimental::CConvertibleBond::CConvertibleBond (PLATFORM::Type^ t) : CBond(CConvertibleBond::typeid)
{
#ifdef HANDLE
	_phConvertibleBond = NULL;
#endif
	if (!t->IsSubclassOf(CConvertibleBond::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::CConvertibleBond::CConvertibleBond (QuantLib::Handle<QuantLib::ConvertibleBond>& childNative, Object^ owner)  : CBond(CConvertibleBond::typeid)
{
	_phConvertibleBond = &childNative;
	_ppConvertibleBond = &static_cast<boost::shared_ptr<QuantLib::ConvertibleBond>>(childNative.currentLink());
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppConvertibleBond));
    _ConvertibleBondOwner = owner;
}
Cephei::QL::Experimental::CConvertibleBond::CConvertibleBond (QuantLib::Handle<QuantLib::ConvertibleBond> childNative)  : CBond(CConvertibleBond::typeid)
{
	_phConvertibleBond = &childNative;
	_ppConvertibleBond = &static_cast<boost::shared_ptr<QuantLib::ConvertibleBond>>(childNative.currentLink());
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppConvertibleBond));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::CConvertibleBond::CConvertibleBond (QuantLib::ConvertibleBond childNative)  : CBond(CConvertibleBond::typeid)
{
#ifdef HANDLE
	_phConvertibleBond = NULL;
#endif
	_ppConvertibleBond = new boost::shared_ptr<QuantLib::ConvertibleBond> (new QuantLib::ConvertibleBond (childNative));
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppConvertibleBond));
}
#endif

Cephei::QL::Experimental::CConvertibleBond::~CConvertibleBond ()
{
    if (_ppConvertibleBond != NULL)
    {
	    delete _ppConvertibleBond;
        _ppConvertibleBond = NULL;
    }
}
Cephei::QL::Experimental::CConvertibleBond::!CConvertibleBond ()
{
    if (_ppConvertibleBond != NULL)
    {
	    delete _ppConvertibleBond;
    }
}
QuantLib::ConvertibleBond& Cephei::QL::Experimental::CConvertibleBond::GetReference ()
{
    if (_ppConvertibleBond == NULL) throw REFNEW NativeNullException ();
	return **_ppConvertibleBond;
}
boost::shared_ptr<QuantLib::ConvertibleBond>& Cephei::QL::Experimental::CConvertibleBond::GetShared ()
{
    if (_ppConvertibleBond == NULL) throw REFNEW NativeNullException ();
	return *_ppConvertibleBond;
}
QuantLib::ConvertibleBond* Cephei::QL::Experimental::CConvertibleBond::GetPointer ()
{
    if (_ppConvertibleBond == NULL) throw REFNEW NativeNullException ();
	return &**_ppConvertibleBond;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ConvertibleBond>& Cephei::QL::Experimental::CConvertibleBond::GetHandle ()
{
	if (_phConvertibleBond == NULL)
	{
		_phConvertibleBond = new Handle<QuantLib::ConvertibleBond> (*_ppConvertibleBond);
	}
	return *_phConvertibleBond;
}
#endif
bool Cephei::QL::Experimental::CConvertibleBond::HasNative () 
{
	return (_ppConvertibleBond != NULL);
}

Cephei::Core::IVector<Cephei::QL::Instruments::ICallability^>^ Cephei::QL::Experimental::CConvertibleBond::Callability::get ()
{
    try
    {
    	std::vector<boost::shared_ptr<QuantLib::Callability> >& _rv = (std::vector<boost::shared_ptr<QuantLib::Callability> >&)(*_ppConvertibleBond)->callability ( );   
        CoVector<Cephei::QL::Instruments::ICallability^>^ _nrv = REFNEW CoVector<Cephei::QL::Instruments::ICallability^>(REFNEW CCallabilityVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::CConvertibleBond::ConversionRatio::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppConvertibleBond)->conversionRatio ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IQuote^ Cephei::QL::Experimental::CConvertibleBond::CreditSpread::get ()
{
    try
    {
    	Handle<QuantLib::Quote>& _rv = (Handle<QuantLib::Quote>&)(*_ppConvertibleBond)->creditSpread ( );   
        Cephei::QL::CQuote^ _nrv = REFNEW Cephei::QL::CQuote (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Cephei::QL::Cashflows::IDividend^>^ Cephei::QL::Experimental::CConvertibleBond::Dividends::get ()
{
    try
    {
    	std::vector<boost::shared_ptr<QuantLib::Dividend> >& _rv = (std::vector<boost::shared_ptr<QuantLib::Dividend> >&)(*_ppConvertibleBond)->dividends ( );   
        CoVector<Cephei::QL::Cashflows::IDividend^>^ _nrv = REFNEW CoVector<Cephei::QL::Cashflows::IDividend^>(REFNEW CDividendVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

